/**
* @file
* @author renzhiqiang
* @date 2017-04-13
*/
<template>
  <div>
    <header id="header" class="mui-bar mui-bar-nav" style="position: fixed;">
      <!--<a class="mui-icon mui-icon-left-nav mui-pull-left" @tap="goBack"></a>-->
      <div class="content mt10">
        <div class="center">影视会员领取</div>
      </div>
      <div class="content mt3">
        <div class="left">该设备绑定账号 : {{data.user_account}}</div>
      </div>
    </header>
    <div class="mui-content bgGrep">
      <div class="mui-scroll-wrapper" style="padding-top: 56px;">
        <div>
        <div class="result">
          <template v-if="isTrue">
            <img src="./../images/fail.svg">
          </template>
          <template v-else="">
            <img  id='default' v-show="data.commentUrl === ''" v-bind:src="data.evaluate">
            <span v-show="data.commentUrl != ''"  id='defaultimgs'></span>
            <!--<img v-show="data.commentUrl != ''"  id='defaultimgs'/>-->
          </template>
        </div>
        <template v-if="isTrue">
          <div class="content" >
            <div class="center">图片获取失败</div>
          </div>
        </template>
        <template v-else>
          <div class="content" >
            <div class="left"><div class="circle blue"></div> 第二步 :  上传您的 "好评" 截图</div>
          </div>
        </template>
        <div  class="mui-icon-load-uplaod mui-btn mui-btn-upload" @tap="selectFile()">{{uplaod}}</div>
        <div  class="mui-btn mui-btn-SingIn" @tap="pre()">上一步</div>
        <div  id='button'  class="mui-btn mui-btn-SingIn" @tap="saveFn()">提交</div>
      </div>
      </div>
    </div>
  </div>
</template>
<script>
  import store from '../submit-store';
  import {tools} from 'tools';
  var $ = require('jquery');
  import {FastClick} from 'fastclick';
  export default {
    components: {},
    data: function () {    // 组件的数据格式
      return {
        data: store.state.loginData,
        isTrue: false,
        uplaod: '上传',
        r: ''
      };
    },
    methods: {
      selectFile: function () {
        var self = this;
        console.info('打样');
        tools.uploadFile('camera', false, function (value) {
          tools.show('图片加载中...');
          var formData = new FormData();
          formData.append('file', value);
          $.ajax(`${store.state.loginData.url}vip/terminalOrderUpload/v2/upload`, {
            method: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function (data) {
              var datas = JSON.parse(data);
              if (datas.status == '2000') {
                self.uplaod = '重新上传';
                self.isTrue = false;
                store.state.loginData.commentUrl = self.data.commentUrl = datas.url;
                tools.createImage('#defaultimgs', value);
//                document.getElementById('defaultimgs').setAttribute('src', datas.url);
                setTimeout(function () {
                  tools.hide();
                }, 1000);
              } else {
                tools.hide();
                self.uplaod = '重新上传';
                self.isTrue = true;
              }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
              tools.hide();
              self.uplaod = '重新上传';
              self.isTrue = true;
            }
          });
        });
      },
      pre: function () {
        this.$router.go('/');
      },
      saveFn: function () {
        if (this.data.commentUrl) {
          var self = this;
          store.saveFn(value => {
            if (value.data.status == 2000) {
              self.$router.go('/Succues');
            } else if (value.data.status == 3003) {
                tools.msgShow(value.data.reason);
            } else {
              self.$router.go('/fail');
            }
          });
        } else {
          tools.msgShow('请上传一张 "好评" 截图');
        };
      }
    },
    route: {
      data: function (data) {
        console.info('11');
        this.isTrue = false;
      }
    },
    ready: function () {
      mui('.mui-scroll-wrapper').scroll();
      FastClick.attach(document.body);
    }
  };
</script>
